<?php include_once ('function/check_login_function.php'); onlyValid(); onlyPatient();?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Health Tracker</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Health Tracker">
    <meta name="author" content="">

    <!-- Le styles -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="shortcut icon" href="ico/favicon.ico">
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
    <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
  </head>

  <body>


    <div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </a>
          <a class="brand" href="index.php" style="margin-top: 5px;">Health Tracker</a>
          <div class="nav-collapse collapse">
          <div class="pull-right">
          <ul class="nav">
		  	<?php if (isset($_SESSION['session'])) {?>
                <li><a href="profile.php">Profile</a></li>
				<li><a href="hospitals.php">Hospitals</a> </li>
				<li><a href="doctors.php">Doctors</a></li>
				<li><a href="calendar.php">Calendar</a></li>
				<li><a href="notifications.php">Notifications</a> </li>

                <?php
                if($user->getRole() === 'doctor'){
                    echo '<li><a href="patients.php">Patients</a> </li>';
                } else if ($user->getRole() === 'patient'){
					echo '<li class="active"><a href="symptoms.php">Symptoms</a></li>';
					echo '<li><a href="food.php">Food</a></li>';
					echo '<li><a href="log.php">Log</a></li>';
				    echo '<li><a href="request.php">Requests</a></li>';
                }
                ?>
				<li><a href="edit.php">Settings</a></li>
            </ul>
				<form action="function/logout_function.php" method="POST" style="display: inline;">
				<input type="submit" name="logout" class="btn btn-primary" value="Logout" />
				</form>
				<?php } else { ?>

					<form action="function/login_function.php" method="POST" style="display:inline">
                    <input type="text" class="input-small" placeholder="Email" id="username" name="username" style="margin-top:8px;"/>
                    <input type="password" class="input-small" placeholder="Password" id="password" name="password" style="margin-top:8px;"/>
                    <button type="submit" name="login" class="btn btn-primary">Login</button>
					</form>
				<?php }  ?>


              </div>
              
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>
      
    <div class="container">
    <div class="row">
    <div class="span8">
      <h1>Request</h1>
				<h2>Symptom</h2>
				<?php if ($user->getRole() === 'patient') {?>
					<?php $symptom_list = $user->getSymptomList(); ?>
					<?php if ($symptom_list !== null) {?>
					<table class="table">
					<tr>
					<th>Name</th>
					<th>Level</th>
					<th>Notes</th>
					<th>Date</th>
					<th></th>
					</tr>
					<?php 
						
							foreach ($symptom_list as $symptom) {
								echo '<tr>';
								echo '<td>' . $symptom['symptom_name'] . '</td>';
								echo '<td>' . $symptom['symptom_level'] . '</td>';
								echo '<td>' . $symptom['symptom_note'] . '</td>';
								echo '<td>' . $symptom['created_at'] . '</td>';
								echo '<td><a href="function/delete_symptom_function.php?id=' . $symptom['symptom_id'] . '">Delete</a></td>';
								echo '</tr>';
							}
						}
					?>
					</table>
					<h2>Log</h2>
					<?php $datalog_list = $user->getDataLogList(); ?>
					<?php if ($datalog_list !== null) {?>
					<table class="table">
					<tr>
					<th>Type</th>
					<th>Value</th>
					<th>Notes</th>
					<th>Date</th>
					<th></th>
					</tr>
					<?php 
						
							foreach ($datalog_list as $datalog) {
								echo '<tr>';
								echo '<td>' . DataLog::findTypeName($datalog['datalog_type_id']) . '</td>';
								echo '<td>' . $datalog['datalog_value'] . '</td>';
								echo '<td>' . $datalog['datalog_note'] . '</td>';
								echo '<td>' . $datalog['created_at'] . '</td>';
								echo '<td><a href="function/delete_log_function.php?id=' . $datalog['datalog_id'] . '">Delete</a></td>';
								echo '</tr>';
							}
						}
					?>
					</table>

					
					<h2>Get Symptom From User</h2>
					<?php $patient_list = Symptom::getBenchmarkPatients(); ?>
					<?php if ($patient_list !== null) {?>
						<table class="table">
						<tr>
						<th>ID</th>
						<th>Name</th>
						<th>Level</th>
						<th>Notes</th>
						<th>Date</th>
						<th>Type</th>
						<th>Value</th>
						<th></th>
						</tr>
						<?php 
							foreach ($patient_list as $patient) {
								$symptom_list = Symptom::getSymptomListFromUser($patient["user_id"]);
								if ($symptom_list !== null) {
									echo '<tr>';
									echo '<td rowspan="' . count($symptom_list) . '">' . $patient["user_id"] .  '</td>';
									foreach ($symptom_list as $symptom) {
										echo '<td>' . $symptom['symptom_name'] . '</td>';
										echo '<td>' . $symptom['symptom_level'] . '</td>';
										echo '<td>' . $symptom['symptom_note'] . '</td>';
										echo '<td>' . $symptom['created_at'] . '</td>';
										echo '<td>' . DataLog::findTypeName($symptom['datalog_type_id']) . '</td>';
										echo '<td>' . $symptom['datalog_value'] . '</td>';
										echo '<td>Delete?</td>';
										echo '</tr>';									
									}
								}
							}							
						?>
						</table>	
					<?php } ?>					

					<h2>Benchmark</h2>
					<form action="symptoms.php" method="GET" class="form-horizontal">
					<label for="benchmark">Filter:</label><input type="text" id="benchmark" class="input-large" name="benchmark"/> 
					<button type="filter" name="filter" class="btn btn-primary" style="margin:10px;">Filter</button>
					</form>
					<hr/>
					
					<?php if (isset($_GET['benchmark'])) { ?>
						<?php $benchmark_list = Symptom::benchmarkSymptoms($user->getUserID(), $_GET['benchmark']); ?>
						<?php if ($benchmark_list !== null) {?>
						<table class="table">
						<tr>
						<th>ID</th>
						<th>Name</th>
						<th>Level</th>
						<th>Notes</th>
						<th>Date</th>
						<th></th>
						</tr>
							<?php 
								foreach ($benchmark_list as $benchmark) {
									echo '<tr>';
									echo '<td>' . $benchmark["user_id"] .  '</td>';
									echo '<td>' . $benchmark['symptom_name'] . '</td>';
									echo '<td>' . $benchmark['symptom_level'] . '</td>';
									echo '<td>' . $benchmark['symptom_note'] . '</td>';
									echo '<td>' . $benchmark['created_at'] . '</td>';
									echo '<td>Delete?</td>';
									echo '</tr>';									
								}					
							?>
						</table>	
						<?php } ?>	
					<?php } ?>			

					<h2>Benchmark with Datalog</h2>
					<form action="symptoms.php" method="GET" class="form-horizontal">
					<label for="benchmark">Filter:</label><input type="text" id="benchmark" class="input-large" name="benchmark"/>
					<?php 
							echo '<select id="datalog" name="datalog">';			
							$datalogtype_list = DataLog::getDataLogTypeList();
							foreach ($datalogtype_list as $datalogtype) {
								echo '<option value="'.$datalogtype['datalog_type_id'].'">'.ucwords($datalogtype['datalog_type_name']).'</option>'; 
							}
							echo '</select>';					
					?>
					<button type="filter" name="filter" class="btn btn-primary" style="margin:10px;">Filter</button>
					</form>
					<hr/>
					
					<?php if (isset($_GET['benchmark']) && isset($_GET['datalog'])) { ?>
						<?php $benchmark_list = Symptom::benchmarkSymptomsWithDatalog($user->getUserID(), $_GET['benchmark'], $_GET['datalog']); ?>
						<?php if ($benchmark_list !== null) {?>
						<table class="table">
						<tr>
						<th>ID</th>
						<th>Name</th>
						<th>Level</th>
						<th>Notes</th>
						<th>Date</th>
						<th></th>
						</tr>
							<?php 
								foreach ($benchmark_list as $benchmark) {
									echo '<tr>';
									echo '<td>' . $benchmark["user_id"] .  '</td>';
									echo '<td>' . $benchmark['symptom_name'] . '</td>';
									echo '<td>' . $benchmark['symptom_level'] . '</td>';
									echo '<td>' . $benchmark['symptom_note'] . '</td>';
									echo '<td>' . $benchmark['created_at'] . '</td>';
									echo '<td>' . DataLog::findTypeName($benchmark['datalog_type_id']) . '</td>';
									echo '<td>' . $benchmark['datalog_value'] . '</td>';
									echo '<td>Delete?</td>';
									echo '</tr>';									
								}					
							?>
						</table>	
						<?php } ?>	
					<?php } ?>		
					
					<form action="function/enter_symptom_function.php" method="POST">				
					<label for="name">Symptom (Optional): </label>
					<input type="text" id="name" name="name" /> <br />
					<input type="radio" id="level" name="level" value="1" checked="checked"/> Mild
					<input type="radio" id="level" name="level" value="2"/> Average 
					<input type="radio" id="level" name="level" value="3"/> Severe <br />
					<label for="note"> Note: </label> <br />
					<textarea id="note" name="note" rows="4" cols="50"></textarea> <br />
					
					Additional information (Optional):  <br />
					<?php 					
						$no_of_datalog = 1;
						
						if (isset($_GET['field']) && intval($_GET['field']) > 0) {
							$no_of_datalog = intval($_GET['field']);
						}
						
						for ($i = 0; $i < $no_of_datalog; $i++) {
							//Add log
							echo '<select id="type" name="type[]">';			
							$datalogtype_list = DataLog::getDataLogTypeList();
							foreach ($datalogtype_list as $datalogtype) {
								echo '<option value="'.$datalogtype['datalog_type_id'].'">'.ucwords($datalogtype['datalog_type_name']).'</option>'; 
							}
							echo '</select>';
							echo '<input type="text" id="value" name="value[]" /> <br />';
						}
						
						//$no_of_datalog += 1;
						echo ' <a href="symptoms.php?field='.++$no_of_datalog.'">Add more</a> <br /> <br />';
					?>
					
					<input type="submit" name="enter" value="Enter" class="btn btn-primary"/> <br /> 
					</form>

				<?php } ?>
    </div>
    </div>
    </div> <!-- /container -->
        <script src="js/jquery.js"></script>
    <script src="js/bootstrap.js"></script>
  </body>
</html>